package ph.com.gs3.formalistics.model.valueobjects.business;

import java.io.Serializable;

import org.json.JSONException;
import org.json.JSONObject;

public class User implements Serializable {

	public static final String TAG = User.class.getSimpleName();

	private static final long serialVersionUID = -3798985922783499024L;

	private static final class WEB_KEY {

		// @formatter:off

		// Web Keys
		public static final String WEB_ID 		= "id"; // this should be separate
														// from the actual id since
														// this may duplicate		
		public static final String EMAIL 		= "email";
		public static final String IMAGE_URL 	= "image_url";
		public static final String DISPLAY_NAME = "display_name";
		public static final String USER_LEVEL 	= "user_level_id";
		
		public static final String COMPANY_ID	= "company_id";
		public static final String COMPANY_NAME = "company_name";
		
		// Application Keys
		// public static final String ID = "_id";
		// public static final String PASSWORD = "password";
		// public static final String SERVER = "server";
		// public static final String IS_ACTIVE = "is_active";

		// @formatter:on

	}

	public static final class DOCUMENT_AUTHOR_KEY {

		// @formatter:off

		// Web Keys
		public static final String WEB_ID 		= "requestor_id"; 	// this should be separate
		                                          					// from the actual id since
		                                          					// this may duplicate
		public static final String EMAIL 		= "requestor_email";
		public static final String IMAGE_URL 	= "requestor_image_url";
		public static final String DISPLAY_NAME = "requestor_display_name";
		public static final String USER_LEVEL 	= "requestor_user_level_id";

		public static final String COMPANY_ID	= "requestor_company_id";
		
		// @formatter:on

	}

	public static final class USER_LEVEL {
		public static final int USER = 3;
		public static final int ADMIN = 2;
	}

	public static final class ACTIVITY {
		public static final int ACTIVE = 1;
		public static final int INACTIVE = 0;
	}

	private int dbId;

	private int webId;
	private String companyId;
	private String companyName;

	private String email;
	private String displayName;
	private String imageURL;
	private String password;
	private int userLevel;

	private String lastAllFormsUpdate;
	private String lastAllDocumentsUpdate;
	private String lastAllCommentsUpdate;

	private String accountServer;
	private int activity;

	public static User createFromJSON(JSONObject rawUserJSON, String server)
	        throws UserParseException {

		User user = new User();

		// FLLogger.d(TAG, rawUserJSON.toString());

		String currentField = "";

		try {
			currentField = WEB_KEY.WEB_ID;
			user.setWebId(rawUserJSON.getInt(currentField));

			currentField = WEB_KEY.EMAIL;
			user.setEmail(rawUserJSON.getString(currentField));

			currentField = WEB_KEY.DISPLAY_NAME;
			user.setDisplayName(rawUserJSON.getString(currentField));

			currentField = WEB_KEY.IMAGE_URL;
			user.setImageURL(rawUserJSON.getString(currentField));

			currentField = WEB_KEY.USER_LEVEL;
			user.setUserLevel(rawUserJSON.getInt(currentField));

			currentField = WEB_KEY.COMPANY_ID;
			user.setCompanyId(rawUserJSON.getString(currentField));

			currentField = WEB_KEY.COMPANY_NAME;
			user.setCompanyName(rawUserJSON.getString(currentField));

		} catch (JSONException e) {
			throw new UserParseException(
			        "Server gave an invalid response. Unable to create user from JSON object, "
			                + currentField + " seems to be missing or has invalid format.");
		}

		user.setAccountServer(server);

		return user;

	}

	public static User createFromDocument(JSONObject documentJSON, String server)
	        throws UserParseException {

		User user = new User();

		String currentField = "";

		try {
			// @formatter:off
			currentField 		= DOCUMENT_AUTHOR_KEY.WEB_ID;
			user.webId			= documentJSON.getInt(currentField);			

			currentField 		= DOCUMENT_AUTHOR_KEY.EMAIL;
			user.email			= documentJSON.getString(currentField);			

			currentField 		= DOCUMENT_AUTHOR_KEY.DISPLAY_NAME;
			user.displayName	= documentJSON.getString(currentField);			

			currentField 		= DOCUMENT_AUTHOR_KEY.IMAGE_URL;
			user.imageURL 		= documentJSON.getString(currentField);

			currentField 		= DOCUMENT_AUTHOR_KEY.USER_LEVEL;
			user.userLevel		= documentJSON.getInt(currentField);
			
			currentField		= DOCUMENT_AUTHOR_KEY.COMPANY_ID;
			user.companyId		= documentJSON.getString(currentField);
			// @formatter:on

		} catch (JSONException e) {
			throw new UserParseException(
			        "Server gave an invalid response. Unable to create user from JSON object, "
			                + currentField + " seems to be missing or has invalid format.");
		}

		user.setAccountServer(server);

		return user;

	}

	@Override
	public String toString() {
		return this.dbId + " " + this.displayName;
	}

	// ========================================================================
	// {{ Exceptions

	public static class UserParseException extends Exception {

		private static final long serialVersionUID = 6015202562655877661L;

		public UserParseException(String message) {
			super(message);
		}

	}

	// }}

	// ========================================================================
	// {{ Getters and Setters

	public int getWebId() {
		return webId;
	}

	public void setWebId(int webId) {
		this.webId = webId;
	}

	public int getId() {
		return dbId;
	}

	public void setId(int id) {
		this.dbId = id;
	}

	public String getCompanyId() {
		return companyId;
	}

	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public String getImageURL() {
		return imageURL;
	}

	public void setImageURL(String imageURL) {
		this.imageURL = imageURL;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getUserLevel() {
		return userLevel;
	}

	public void setUserLevel(int userLevel) {
		this.userLevel = userLevel;
	}

	public String getAccountServer() {
		return accountServer;
	}

	public void setAccountServer(String accountServer) {

		if (accountServer != null) {
			accountServer = accountServer.trim();
		} else {
			accountServer = "";
		}

		// make sure that the server contains http
		if (!accountServer.contains("http://") && !accountServer.isEmpty()) {
			this.accountServer = "http://" + accountServer;
		} else {
			this.accountServer = accountServer;
		}
	}

	public String getLastAllFormsUpdate() {
		return lastAllFormsUpdate;
	}

	public void setLastAllFormsUpdate(String lastAllFormsUpdate) {
		this.lastAllFormsUpdate = lastAllFormsUpdate;
	}

	public String getLastAllDocumentsUpdate() {
		return lastAllDocumentsUpdate;
	}

	public void setLastAllDocumentsUpdate(String lastAllDocumentsUpdate) {
		this.lastAllDocumentsUpdate = lastAllDocumentsUpdate;
	}

	public String getLastAllCommentsUpdate() {
		return lastAllCommentsUpdate;
	}

	public void setLastAllCommentsUpdate(String lastAllCommentsUpdate) {
		this.lastAllCommentsUpdate = lastAllCommentsUpdate;
	}

	public int getActivity() {
		return activity;
	}

	public void setActivity(int activity) {
		this.activity = activity;
	}

	// }}

}
